/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util;

import java.io.OutputStream;
import java.io.Writer;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import ca.forklabs.baselib.util.iterator.ArrayIterator;
import ca.forklabs.baselib.util.iterator.BackInsertIterator;
import ca.forklabs.baselib.util.iterator.ConstantIterator;
import ca.forklabs.baselib.util.iterator.EnumerationIterator;
import ca.forklabs.baselib.util.iterator.FrontInsertIterator;
import ca.forklabs.baselib.util.iterator.InsertIterator;
import ca.forklabs.baselib.util.iterator.IterableWrapper;
import ca.forklabs.baselib.util.iterator.IteratorEnumeration;
import ca.forklabs.baselib.util.iterator.PrintIterator;
import ca.forklabs.baselib.util.iterator.RangedIterator;
import ca.forklabs.baselib.util.iterator.SingleObjectEnumeration;
import ca.forklabs.baselib.util.iterator.SingleObjectIterator;
import ca.forklabs.baselib.util.iterator.TransformerIterator;

/**
 * Class {@code Iterators} provides factory methods to create {@link Iterator}s,
 * {@link Enumeration}s or {@link Iterable}s with special behaviour.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.Iterators">Daniel Léonard</a>
 * @version $Revision$
 */
public class Iterators {

//---------------------------
// Constructor
//---------------------------

   /**
    * Let no one instanciate this class.
    */
   protected Iterators() {
   // nothing
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates an iterator over the given array.
    * @param   <E>   the type of the elements in the array.
    * @param   array   the array.
    * @return   the iterator.
    */
   @SafeVarargs
   public static <E> ListIterator<E> asIterator(E... array) {
      ListIterator<E> iter = Iterators.asIterator(array, 0);
      return iter;
      }

   /**
    * Creates an iterator over the given array.
    * @param   <E>   the type of the elements in the array.
    * @param   array   the array.
    * @param   index   the starting index.
    * @return   the iterator.
    */
   public static <E> ListIterator<E> asIterator(E[] array, int index) {
      ListIterator<E> iter = new ArrayIterator<>(array, index);
      return iter;
      }

   /**
    * Creates an {@link Iterable} over the given array.
    * @param   <E>   the type of the elements in the array.
    * @param   array   the array.
    * @return   the iterable.
    */
   @SafeVarargs
   public static <E> Iterable<E> asIterable(E... array) {
      List<E> list = java.util.Arrays.asList(array);
      return list;
      }

   /**
    * Transforms the given {@link Iterator} into an {@link Iterable}.
    * @param    <E>   the type of the elements in the iterator.
    * @param   iterator   the iterator.
    * @return   an iterable.
    */
   @SuppressWarnings("unchecked")
   public static <E> Iterable<E> asIterable(Iterator<E> iterator) {
      Iterable<E> iterable = (iterator instanceof Iterable) ? (Iterable<E>) iterator : new IterableWrapper<>(iterator);
      return iterable;
      }

   /**
    * Transforms the given {@link Enumeration} into an {@link Iterable}.
    * @param    <E>   the type of the elements in the enumeration.
    * @param   enumeration   the enumeration.
    * @return   an iterable.
    */
   public static <E> Iterable<E> asIterable(Enumeration<E> enumeration) {
      Iterator<E> iterator = Iterators.asIterator(enumeration);
      Iterable<E> iterable = Iterators.asIterable(iterator);
      return iterable;
      }

   /**
    * Transforms the given {@link Iterable} into an {@link Iterator}.
    * @param    <E>   the type of the elements in the iterable.
    * @param   iterable   the iterable.
    * @return   an iterator.
    */
   public static <E> Iterator<E> asIterator(Iterable<E> iterable) {
      Iterator<E> iterator = iterable.iterator();
      return iterator;
      }

   /**
    * Transforms the given {@link Enumeration} into an {@link Iterator}.
    * @param    <E>   the type of the elements in the enumeration.
    * @param   enumeration   the enumeration.
    * @return   an iterator.
    */
   public static <E> Iterator<E> asIterator(Enumeration<E> enumeration) {
      Iterator<E> iterator = new EnumerationIterator<>(enumeration);
      return iterator;
      }

   /**
    * Transforms the given {@link Iterator} into an {@link Enumeration}.
    * @param    <E>   the type of the elements in the iterator.
    * @param   iterator   the iterator.
    * @return   an enumeration.
    */
   public static <E> Enumeration<E> asEnumeration(Iterator<E> iterator) {
      Enumeration<E> enumeration = new IteratorEnumeration<>(iterator);
      return enumeration;
      }

   /**
    * Creates an enumeration over the given array.
    * @param   <E>   the type of the elements in the array.
    * @param   array   the array.
    * @return   an enumeration.
    */
   @SafeVarargs
   public static <E> Enumeration<E> asEnumeration(E... array) {
      Enumeration<E> enumeration = Iterators.asEnumeration(array, 0);
      return enumeration;
      }

   /**
    * Creates an enumeration over the given array.
    * @param   <E>   the type of the elements in the array.
    * @param   array   the array.
    * @param   index   the starting index.
    * @return   an enumeration.
    */
   public static <E> Enumeration<E> asEnumeration(E[] array, int index) {
      Iterator<E> iterator = Iterators.asIterator(array, index);
      Enumeration<E> enumeration = Iterators.asEnumeration(iterator);
      return enumeration;
      }

   /**
    * Creates an enumeration over the single object.
    * @param   <E>   the type of the object.
    * @param   single_object   the object.
    * @return   an enumeration.
    */
   public static <E> Enumeration<E> asEnumeration(E single_object) {
      Enumeration<E> enumeration = new SingleObjectEnumeration<>(single_object);
      return enumeration;
      }

   /**
    * Transforms the given object into an {@link Iterator}.
    * @param    <E>   the type of the object.
    * @param   single_object   the object.
    * @return   an iterator.
    */
   public static <E> ListIterator<E> asIterator(E single_object) {
      ListIterator<E> iterator = new SingleObjectIterator<>(single_object);
      return iterator;
      }

   /**
    * Modifies the iterator to append instead of changing the current element.
    * @param   <E>   the type of the elements in the collection.
    * @param   iterator   the iterator to modify the behaviour.
    * @return   an inserter iterator.
    * @see   InsertIterator
    */
   public static <E> ListIterator<E> inserter(ListIterator<E> iterator) {
      InsertIterator<E> inserter = InsertIterator.create(iterator);
      return inserter;
      }

   /**
    * Gets a back insert iterator on the given collection.
    * @param   <E>   the type of the elements in the collection.
    * @param   collection   the collection the iterator will append to.
    * @return   a back insert iterator.
    * @see   BackInsertIterator
    */
   public static <E> ListIterator<E> backInserter(Collection<E> collection) {
      ListIterator<E> back_inserter = new BackInsertIterator<>(collection);
      return back_inserter;
      }

   /**
    * Gets a front insert iterator on the given list.
    * @param   <E>   the type of the elements in the list.
    * @param   list   the list the iterator will prepend on.
    * @return   a front insert iterator.
    * @see   FrontInsertIterator
    */
   public static <E> ListIterator<E> frontInserter(List<E> list) {
      ListIterator<E> back_inserter = new FrontInsertIterator<>(list);
      return back_inserter;
      }

   /**
    * Creates an iterator that will return at most <em>max</em> elements.
    * @param   <E>   the type of the elements in the iterable.
    * @param   iterable   the collection.
    * @param   max   the max number of elements
    * @return   an iterator.
    */
   public static <E> Iterator<E> range(Iterable<E> iterable, int max) {
      Iterator<E> iterator = iterable.iterator();
      Iterator<E> ranged = new RangedIterator<>(iterator, max);
      return ranged;
      }

   /**
    * Creates a print iterator with the space character as delimiter.
    * @param   <E>   the type of the elements to print.
    * @param   writer   the writer to write into.
    * @return   the print iterator.
    */
   @SuppressWarnings("nls")
   public static <E> ListIterator<E> printIterator(Writer writer) {
      ListIterator<E> ouput_iterator = Iterators.printIterator(writer, " ");
      return ouput_iterator;
      }

   /**
    * Creates a print iterator.
    * @param   <E>   the type of the elements to print.
    * @param   writer   the writer to write into.
    * @param   delimiter   the delimiter between elements.
    * @return   the print iterator.
    */
   public static <E> ListIterator<E> printIterator(Writer writer, String delimiter) {
      ListIterator<E> ouput_iterator = new PrintIterator<>(writer, delimiter);
      return ouput_iterator;
      }

   /**
    * Creates a print iterator with the space character as delimiter.
    * @param   <E>   the type of the elements to print.
    * @param   out   the stream to write into.
    * @return   the print iterator.
    */
   @SuppressWarnings("nls")
   public static <E> ListIterator<E> printIterator(OutputStream out) {
      ListIterator<E> ouput_iterator = Iterators.printIterator(out, " ");
      return ouput_iterator;
      }

   /**
    * Creates a print iterator.
    * @param   <E>   the type of the elements to print.
    * @param   out   the stream to write into.
    * @param   delimiter   the delimiter between elements.
    * @return   the print iterator.
    */
   public static <E> ListIterator<E> printIterator(OutputStream out, String delimiter) {
      ListIterator<E> ouput_iterator = new PrintIterator<>(out, delimiter);
      return ouput_iterator;
      }

   /**
    * Creates a new {@link TransformerIterator}.
    * @param   function   the transforming function.
    * @param   iterator   the source iterator.
    * @return   a transformer iterator.
    */
   public static <R, A> Iterator<R> transformer(UnaryFunction<R, A> function, Iterator<A> iterator) {
      Iterator<R> transformer = new TransformerIterator<>(function, iterator);
      return transformer;
      }

   /**
    * Creates a new {@link TransformerIterator}.
    * @param   function   the transforming function.
    * @param   iterable   the source.
    * @return   a transformer iterator.
    */
   public static <R, A> Iterator<R> transformer(UnaryFunction<R, A> function, Iterable<A> iterable) {
      Iterator<A> iterator = asIterator(iterable);
      Iterator<R> transformer = transformer(function, iterator);
      return transformer;
      }

   /**
    * Creates a new {@link TransformerIterator} disguised as an
    * {@link Iterable}.
    * @param iterable   the source.
    * @param function   the transforming function.
    * @return   a transformer iterable.
    */
   public static <R, A> Iterable<R> transformer(Iterable<A> iterable, UnaryFunction<R, A> function) {
      Iterator<R> iterator = transformer(function, iterable);
      Iterable<R> iter = asIterable(iterator);
      return iter;
      }

   /**
    * Creates a new {@link ConstantIterator}.
    * @param   value   the value.
    * @return   a constant iterator.
    */
   public static <E> Iterator<E> constant(E value) {
      Iterator<E> iterator = ConstantIterator.create(value);
      return iterator;
      }

   }
